const app = getApp()
let qCloud = require("../../../index.js");

Page({
    /** 页面的初始数据 */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        processId: null,
        processName: "",
        processInfo: {
            processId: null,
            processCode: null,
            processName: "",
            processPrice: null,
            processTotal: null,
            completedQuantity: null,
            unfinishedQuantity: null,
            unitPrice: null,
            submitQuantity: null,
            deletedMark: false,
            subProcessList: []
        },
        subProcessList: []
    },

    // 工序
    processNameInput(e) {
        let self = this;
        let processName = e.detail.value;
        self.data.processInfo["processName"] = processName;
        self.setData({
            processName: processName,
            processInfo: self.data.processInfo
        });
    },

    // 子工序
    subProcessNameInput(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        self.data.subProcessList[index].processName = e.detail.value;
        self.setData({
            subProcessList: self.data.subProcessList
        });
    },

    // 子工序-单价
    subProcessPriceInput(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        self.data.subProcessList[index].processPrice = e.detail.value;
        self.setData({
            subProcessList: self.data.subProcessList
        });
    },

    // 添加工序
    addTap(e) {
        let self = this;
        let subProcess = {
            processId: null,
            processCode: null,
            processName: "",
            processPrice: null,
            processTotal: null,
            completedQuantity: null,
            unfinishedQuantity: null,
            unitPrice: null,
            submitQuantity: null,
            deletedMark: false,
            subProcessList: []
        };
        let subProcessList = self.data.subProcessList;
        subProcessList.push(subProcess);
        self.setData({
            subProcessList: subProcessList
        });
    },

    // 删除工序
    delTap(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let subProcessList = self.data.subProcessList;

        if (subProcessList[index].processId) {
            // 工序ID不为空，则代表为修改项，那么就修改为被删除
            subProcessList[index].deletedMark = true;
        } else {
            // 工序ID为空，则代表为新增项，那么就直接移除
            subProcessList.splice(index, 1);
        }

        self.setData({
            subProcessList: subProcessList
        });
    },

    /** 提交工序 */
    submitProcess(e) {
        let self = this;

        let processName = self.data.processName;
        if (!processName) {
            wx.showToast({
                title: "工序模板名称不能为空",
                icon: "error"
            });
            return;
        }
        let subProcessList = self.data.subProcessList;
        if (!subProcessList || subProcessList.length == 0) {
            wx.showToast({
                title: "最少需要有一道工序",
                icon: "error"
            });
            return;
        }

        let newSubProcessList = [];
        for (let i = 0; i < subProcessList.length; i++) {
            let subProcess = subProcessList[i];
            if (subProcess.processName != "") {
                newSubProcessList.push(subProcess);
            }
        }
        if (!newSubProcessList || newSubProcessList.length == 0) {
            wx.showToast({
                title: "最少需要有一道工序",
                icon: "error"
            });
            return;
        }

        self.data.processInfo["subProcessList"] = newSubProcessList;
        let params = {
            processInfo: self.data.processInfo
        }

        app.fetchApis(self, qCloud.api.common_submitProcess, params, "POST", function (e) {
            let result = e.data;
            if (result.code == 200) {
                wx.navigateBack({
                    delta: 1
                });
            } else {
                wx.showToast({
                    title: result.msg,
                    icon: "error"
                });
            }
        });
    },

    /** 生命周期函数--监听页面加载 */
    onLoad: function (options) {
        let self = this;
        if (options.processId) {
            self.setData({
                processId: options.processId
            });
            self.queryProcessDetails();
        }
    },

    /** 查询工序详情 */
    queryProcessDetails: function (e) {
        let self = this;
        let params = {
            processId: self.data.processId
        };
        app.fetchApis(self, qCloud.api.common_queryProcessDetails, params, "POST", function (e) {
            let result = e.data;
            if (result.code == 200) {
                let data = result.data;
                self.setData({
                    processName: data.processInfo.processName,
                    processInfo: data.processInfo,
                    subProcessList: data.processInfo.subProcessList
                });
            }
        });
    },
    /** 生命周期函数--监听页面初次渲染完成 */
    onReady: function () {},
    /** 生命周期函数--监听页面显示 */
    onShow: function () {},
    /** 生命周期函数--监听页面隐藏 */
    onHide: function () {},
    /** 生命周期函数--监听页面卸载 */
    onUnload: function () {},
    /** 页面相关事件处理函数--监听用户下拉动作 */
    onPullDownRefresh: function () {},
    /** 页面上拉触底事件的处理函数 */
    onReachBottom: function () {},
    /** 用户点击右上角分享 */
    onShareAppMessage: function () {}
});
